# -*- coding: utf-8 -*-

import time

from flask_restful import fields


def ts_func():
    return int(time.time())


from .room import Room, RoomRobot, RoomPlayer
from .robot import Robot
from .player import Player, PlayerGameset, Agent, AgentPlayer, ExceptionalGrabber
from .game import Gameset, Transaction
from .setting import Setting
from .statistics import Statistics


class Marshal(object):
    agent = {
        'wxid': fields.String,
        'alias': fields.String,
        'nickname': fields.String,
        'headimgurl': fields.String,
        'agentedWagerAmount': fields.Integer(attribute='agented_wager_amount'),
    }
    exceptional_grabber = {
        'wxid': fields.String,
        'alias': fields.String,
        'nickname': fields.String,
        'headimgurl': fields.String,
    }
    player = {
        'wxid': fields.String,
        'alias': fields.String(attribute=lambda p: p.alias if p.alias else p.wxid),
        'nickname': fields.String,
        'headimgurl': fields.String,
        'wagerCount': fields.Integer(attribute='wager_count'),
        'wagerAmount': fields.Integer(attribute='wager_amount'),
        'earnedAmount': fields.Integer(attribute='earned_amount'),
        'grabAmount': fields.Integer(attribute='grab_amount'),
        'upPoint': fields.Integer(attribute='up_point'),
        'downPoint': fields.Integer(attribute='down_point'),
        'totalPoint': fields.Integer(attribute='total_point'),
        'agent': fields.Nested(agent)
    }
    gameset = {
        'id': fields.String,
        'sn': fields.String,
        'playerCount': fields.Integer(attribute='player_count'),
        'envelopeCount': fields.Integer(attribute='envelope_count'),
        'envelopeAmount': fields.Integer(attribute='envelope_amount'),
        'wagerAmount': fields.Integer(attribute='wager_amount'),
        'deduction': fields.Integer,
        'bankerId': fields.String(attribute='banker_wxid'),
        'banker': fields.String(attribute=lambda gs: '' if gs.banker is None else gs.banker.nickname),
        'roomId': fields.String(attribute='room_id')
    }
    player_gameset = {
        'id': fields.String,
        'wagerAmount': fields.Integer(attribute='wager_amount'),
        'envelopeAmount': fields.String(attribute='envelope_amount'),
        'grabPoint': fields.String(attribute='grab_point'),
        'grabAmount': fields.Integer(attribute='grab_amount'),
        'earnedAmount': fields.Integer(attribute='earned_amount'),
        'isBanker': fields.Boolean(attribute='is_banker'),
        'playerId': fields.String(attribute='player_wxid'),
        'playerNickname': fields.String(attribute='player.nickname'),
        'sn': fields.String(attribute='gameset.sn'),
    }
    financing = {
        'id': fields.String,
        'wagerAmount': fields.Integer(attribute='wager_amount'),
        'earnedAmount': fields.Integer(attribute='earned_amount'),
        'upPoint': fields.Integer(attribute='up_point'),
        'downPoint': fields.Integer(attribute='down_point'),
        'totalPoint': fields.Integer(attribute='total_point'),
        'envelopeCount': fields.Integer(attribute='envelope_count'),
        'envelopeAmount': fields.Integer(attribute='envelope_amount'),
        'deduction': fields.Integer,
    }